package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.UUID;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacUserResourceDao;
import org.scboy.rbac.service.UserResourceService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserResourceServiceImp implements UserResourceService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model model) {
		RbacUserResourceDao dao=factory.getDao(RbacUserResourceDao.class);
		return dao.queryPage(model);
	}

	@Override
	public boolean add(String userId, String roleId) {
		RbacUserResourceDao dao=factory.getDao(RbacUserResourceDao.class);
		Model oldModel=dao.get(Model.create().add("userId", userId).add("roleId", roleId));
		if(oldModel!=null)return false;
		Model model=Model.create().add("id", UUID.randomUUID().toString())
				.add("userId", userId)
				.add("roleId", roleId)
				.add("addTime", new Date())
				.add("editTime", new Date());
		int result=dao.insert(model);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean delete(String id) {
		RbacUserResourceDao dao=factory.getDao(RbacUserResourceDao.class);
		int result=dao.delete(Model.create().add("id", id));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean deleteByResId(String resId) {
		RbacUserResourceDao dao=factory.getDao(RbacUserResourceDao.class);
		int result=dao.delete(Model.create().add("resId", resId));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean deleteByUserId(String userId) {
		RbacUserResourceDao dao=factory.getDao(RbacUserResourceDao.class);
		int result=dao.delete(Model.create().add("userId", userId));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

}
